﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Resources;
using System.Xml.Linq;
using System.Xml;

namespace MyData.ExpressionBuilder
{
  
  public class ExpressionMapping
  {
    public ExpressionMapping(string path, XElement extensionElement, string converterTypeName,
      string builderTypeName, string itemsProviderTypeName)
    {
      this.Path = path;
      this.ExtensionElement = extensionElement;
      
      this.ConverterTypeName = converterTypeName;
      this.BuilderTypeName = builderTypeName;
      this.ItemsProviderTypeName = itemsProviderTypeName;
    }

    public string Path { get; set; }
    public string ConverterTypeName { get; set; }
    public string BuilderTypeName { get; set; }
    public string ItemsProviderTypeName { get; set; }

    public XElement ExtensionElement { get; set; }
  }

  public static class ExpressionMappings
  {
    //Lower case enums to match XML atributes
    public enum ExtensionAttributes
    {
      builderType, converterType, itemsProviderType
    }

    public static Dictionary<string, ExpressionMapping> Fill(string value)
    {
       
      return (from MapElem in XDocument.Load(new StringReader(value)).Element("ExpressionMap").Elements("ModelItem")
              select new ExpressionMapping(
                 (string)MapElem.Attribute("path"),
                 (XElement)MapElem,
                 (string)MapElem.Attribute("converterType"),
                 (string)MapElem.Attribute("builderType"),
                 (string)MapElem.Attribute("itemsProviderType")))
                      .ToDictionary(m => m.Path);
    }
  }
}

